<template>
  <div class="app-container">
    <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px">
      <el-form-item label="养护名称" prop="ip">
        <el-input v-model="queryParams.maintain_name" placeholder="请输入养护名称" clearable @keyup.enter.native="handleQuery"
          size="small" />
      </el-form-item>
      <el-form-item label="养护类型" prop="maintain_type">
        <el-select size="small" v-model="queryParams.maintain_type" placeholder="请选养护类型" clearable>
          <el-option
              v-for="dict in maintain_types"
              :key="dict.maintain_type"
              :label="dict.maintain_name"
              :value="dict.maintain_type"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['nssc:maintain:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['nssc:maintain:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['nssc:maintain:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--  数据列表区域-->
    <el-table v-loading="loading" :data="maintaintList" @selection-change="handleSelectionChange"
      :cell-style="{ borderColor: 'rgb(1 5 43)' }" :header-cell-style="{ borderColor: 'rgb(1 5 43)' }">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="养护类型" align="center" prop="maintain_type">
        <template slot-scope="scope">
        <span v-if="scope.row.maintain_type == 0">维修</span>
        <span v-else>保养</span>
        </template>
      </el-table-column>
      <el-table-column label="养护名称" align="center" prop="maintain_name" />
      <el-table-column label="养护时间" align="center" prop="maintain_time">
        <template slot-scope="scope">
          <span >{{ parseTime(scope.row.maintain_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="养护周期" align="center" prop="maintain_circle">
      <template slot-scope="scope">
        <span >{{scope.row.maintain_circle}}周</span>
      </template>
      </el-table-column>
      <el-table-column label="养护内容" align="center" prop="maintain_content" />
<!--      <el-table-column label="养护提醒" align="center" prop="maintain_state">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.maintain_state == 1" style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background: #811212!important;border: 1px solid #024697!important;color: #fff!important;">需养护</el-tag>
          <el-tag v-else style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background: #075d9a!important;border: 1px solid #024697!important;color: #fff!important;">不需养护</el-tag>
        </template>
      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="create_time">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['nssc:maintain:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);" />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="养护类型" prop="maintain_type">
              <el-select v-model="form.maintain_type" placeholder="请选养护类型" clearable>
                <el-option
                    v-for="dict in maintain_types"
                    :key="dict.maintain_type"
                    :label="dict.maintain_name"
                    :value="dict.maintain_type"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护名称" prop="maintain_name">
              <el-input v-model="form.maintain_name" placeholder="请输入养护名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护时间">
              <el-date-picker
                  v-model="form.maintain_time"
                  style="width: 194px" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                  type="date"
                  placeholder="请选养护时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护周期" prop="maintain_circle">
              <el-select v-model="form.maintain_circle" placeholder="请输入养护周期" clearable>
                <el-option
                    v-for="dict in maintain_circles"
                    :key="dict.maintain_circle"
                    :label="dict.maintain_circle_name"
                    :value="dict.maintain_circle"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="养护内容" prop="maintain_content">
              <el-input v-model="form.maintain_content" placeholder="请输入养护内容(0-255)" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title1" :visible.sync="open1" width="900px">
      <el-table class="tabled1" v-loading="loading1" :data="maintaintList1" @selection-change="handleSelectionChange"
                :cell-style="{ borderColor: 'rgb(1 5 43)' }" :header-cell-style="{ borderColor: 'rgb(1 5 43)' }">
        <el-table-column label="养护类型" align="center" prop="maintain_type" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.maintain_type == 0">维修</span>
            <span v-else>保养</span>
          </template>
        </el-table-column>
        <el-table-column label="养护名称" align="center" prop="maintain_name"/>
        <el-table-column label="养护内容" align="center" prop="maintain_content" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open1 = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listmaintain,listmaintain1, getmaintain,addmaintain,updatemaintain,delmaintain,updaterepaire } from "@/api/nssc/maintain/maintain";
import "@/assets/styles/robt-bg.css";
export default {
  data() {
    return {
      //养护类型
      maintain_types: [
        {maintain_name:"维修",maintain_type:"0"},
        {maintain_name:"保养",maintain_type:"1"}
      ],
      //养护周期
      maintain_circles: [
        {maintain_circle:"1",maintain_circle_name:"一周"},
        {maintain_circle:"2",maintain_circle_name:"二周"},
        {maintain_circle:"3",maintain_circle_name:"三周"},
        {maintain_circle:"4",maintain_circle_name:"四周"},
        {maintain_circle:"5",maintain_circle_name:"五周"}
      ],
      // 遮罩层
      loading: true,
      loading1: true,
      // 选中数组
      ids: [],
      //复选删除时弹框内容
      maintain_names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      maintaintList: [],
      //表格数据
      maintaintList1: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title1: "",
      // 是否显示弹出层
      open1: false,
      timer:null,//定时器
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        maintain_content: [
          { required: true, message: "养护内容不能为空", trigger: "blur" }
        ] ,
        maintain_circle: [
          { required: true, message: "养护周期不能为空", trigger: "blur" }
        ],
        maintain_name: [
          { required: true, message: "养护名称不能为空", trigger: "blur" }
        ],
        maintain_time: [
          { required: true, message: "养护时间不能为空", trigger: "blur" }
        ],
        maintain_type: [
          { required: true, message: "养护类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  //初始化配置列表
  created() {
    this.getList();
    this.getmaintainList();
  },
  mounted() {
    //定时器
    this.timer = setInterval(()=>{
      this.getmaintainList();
      console.log("监测是否维保信息")
    },6000);//6秒
  },
  beforeDestroy() {
    //清除定时器
    if(this.timer) {
      clearInterval(this.timer); //关闭
    }
  },
  methods: {
    getmaintainList() {
     // this.loading1 = true;
      listmaintain1(this.queryParams).then(response => {
        this.maintaintList1 = response.rows;
        this.loading1 = false;
        if(response.rows!="") {
          this.open1 = true;
          this.title1 = "维保通知";
          //如果养护时间加上周期和当前时间一样，将养护时间更新为当前时间。否则不更新。
          const repairid =[];
          for(let i = 0; i < response.rows.length; i++){
             repairid[i] = response.rows[i].id;
          }
          this.updaterepaired(repairid);//更新保养周期
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增维保";
    },
    //更新用户时间
    updaterepaired(repairid) {
      const ids = repairid;
      updaterepaire(ids).then(response => {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getmaintain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维保";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatemaintain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addmaintain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 查询布防结果列表
    getList() {
      this.loading = true;
      listmaintain(this.queryParams).then(response => {
        this.maintaintList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },// 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },//搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }, //重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }, // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.maintain_names = selection.map(item => item.maintain_name)
      this.single = selection.length != 1
      this.multiple = !selection.length
    }, // 删除按钮操作
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function () {
        return delmaintain(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
<style>
/**列表添加垂直隐藏滚动条**/
.tabled1{
  height: 172px;
  overflow: hidden;
  overflow-y: scroll;
}
.tabled1::-webkit-scrollbar {
  width: 0 !important;
  height: 0 !important;
}
</style>
